﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CRM.Entity;
using CRM.BLL;
using System.Text;
using DP.Common;
using DP.Web.UI;

namespace CRM.Web.MessagingManage
{
    public partial class NoteShow : Common.PageBase
    {
        private UserLoginInfo _userLoginInfo;
        T_NOTE_MSG_BLL bll = new T_NOTE_MSG_BLL();
        T_USER_BLL dal = new T_USER_BLL();
        protected override void OnInit(EventArgs e)
        {
            base.VerificationRight = false;
            base.OnInit(e);
            UserLoginInfo.CheckLoginInfo();
            _userLoginInfo = UserLoginInfo.GetLoginInfo();
            PageHelper.SetPrePageUrl(this.Page);
            PageHelper.SetGoBackUrl(this.Page, btnCancel, StaticConstant.NotBackUrl);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                pageDataInit();
            }
        }

        private void pageDataInit()
        {
            T_NOTE_MSG_Entity entity = new T_NOTE_MSG_Entity();
            entity = bll.GetEntity("AUTOID", PageHelper.GetQueryString(this.Page, "AUTOID"));
            if (entity != null)
            {
                PageHelper.SetControlValue(this.Page, entity);
                entity.STATUS = 1;
                bll.Update(entity);
                if (entity.RECVID == _userLoginInfo.UserEntity.USERID.ToString())
                {
                    this.btnReply.Visible = true;
                }
                else
                {
                    this.btnReply.Visible = false;
                }
                btnOK.Visible = false;
                   
            }
        }

        /// <summary>
        /// Handles the Click event of the btnUpload control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            string filepath = UploadFile(this.Page, ImportFile);
            string filename = GetFileName(filepath);
            if (string.IsNullOrEmpty(filepath))
            {
                PageHelper.ShowMessage(this.Page, "文件上传失败！");
                return;
            }

            btnOK.Enabled = true;
            txtFILEPATH.Text = filepath;
            lblFILES.Text = GetFileName(ImportFile.PostedFile.FileName);
        }

        /// <summary>
        /// 文件 上传方法
        /// </summary>
        /// <param name="page">当前页面</param>
        /// <param name="auc">上传控件</param>
        /// <returns>上传后的文件的服务器路径</returns>
        private string UploadFile(Page page, System.Web.UI.WebControls.FileUpload auc)
        {
            string temp = DateTime.Now.ToString("yyyyMMddHHmmss");
            string filename = auc.PostedFile.FileName;
            filename = GetFileName(filename);

            string filepath = HttpContext.Current.Request.MapPath(StaticConstant.UploadFilePath);

            int index = filename.LastIndexOf('.');
            if (index != -1)
            {
                filename = filename.Insert(index, temp);
            }
            else
            {
                filename += temp;
            }

            string fullpath = string.Empty;
            if (filepath.LastIndexOf("\\") == filepath.Length - 1)
            {
                fullpath = filepath + filename;
            }
            else
            {
                fullpath = filepath + "\\" + filename;
            }

            try
            {
                auc.PostedFile.SaveAs(fullpath);
            }
            catch
            {
                fullpath = null;
            }
            return fullpath;
        }

        /// <summary>
        /// 在绝对路径中，获取Excel文件名
        /// </summary>
        /// <param name="input">需要导入的Excel绝对路径</param>
        /// <returns></returns>
        private string GetFileName(string input)
        {
            return input.Substring(input.LastIndexOf("\\") + 1);
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            long rel = 0;
            if (this.txtSUBJECT.Text.Trim() == "")
            {
                PageHelper.ShowMessage(this.Page, "请输入消息主题！");
                return;
            }

            if (this.txtCONTENT.Text.Trim() == "")
            {
                PageHelper.ShowMessage(this.Page, "请输入消息内容！");
                return;
            }

            T_NOTE_MSG_Entity entity=new T_NOTE_MSG_Entity();
            entity = bll.GetEntity("AUTOID", PageHelper.GetQueryString(this.Page, "AUTOID"));
            if (entity != null)
            {
                T_USER_Entity UserEntity;
                UserEntity = dal.GetCache_T_USER_EntityByKey(this.Page, StringHelper.ToInt(entity.SENDID));
                if (UserEntity != null)
                {
                    entity = new T_NOTE_MSG_Entity();
                    entity.CONTENT = this.txtCONTENT.Text.Trim();
                    entity.FILES = this.lblFILES.Text;
                    entity.STATUS = 0;//表示新消息
                    entity.SUBJECT = this.txtSUBJECT.Text.Trim();
                    entity.FILEPATH = this.txtFILEPATH.Text.Trim();

                    entity.SEND_BUSINESSDIVISIONCODE = _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE;
                    entity.SEND_BUSINESSDIVISIONID = _userLoginInfo.UserEntity.BUSINESSDIVISIONID;
                    entity.SEND_BUSINESSDIVISIONNAME = _userLoginInfo.UserEntity.BUSINESSDIVISIONNAME;
                    entity.CREATETIME = DateTime.Now;
                    entity.SENDID = _userLoginInfo.UserEntity.USERID.ToString();
                    entity.SENDNAME = _userLoginInfo.UserEntity.USERNAME;

                    entity.RECV_BUSINESSDIVISIONCODE = UserEntity.BUSINESSDIVISIONCODE;
                    entity.RECV_BUSINESSDIVISIONID = UserEntity.BUSINESSDIVISIONID;
                    entity.RECV_BUSINESSDIVISIONNAME = UserEntity.BUSINESSDIVISIONNAME;
                    entity.RECVID = UserEntity.USERID.ToString();
                    entity.RECVNAME = UserEntity.USERNAME;

                    rel = bll.Insert(entity);

                    if (rel > 0)
                    {
                        btnOK.Visible = false;
                        PageHelper.ShowMessage(this.Page, "内部消息回复成功！", "NoteSentList.aspx");
                    }
                    else
                    {
                        PageHelper.ShowMessage(this.Page, "内部消息回复失改！");
                    }  
                }
            }
        }

        protected void btnReply_Click(object sender, EventArgs e)
        {
            PageHelper.ClearControlValue(this.Page);
            this.txtCREATETIME.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            T_NOTE_MSG_Entity entity = new T_NOTE_MSG_Entity();
            entity = bll.GetEntity("AUTOID", PageHelper.GetQueryString(this.Page, "AUTOID"));
            if (entity != null)
            {
                txtSENDNAME.Text = entity.RECVNAME;
                txtRECVNAME.Text = entity.SENDNAME;

                btnOK.Visible = true;
            }
        }
    }
}
